package com.gxa.community.customer.jms;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.gxa.community.customer.config.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.util.StringUtils;
import javax.jms.JMSException;
import javax.jms.Message;
import java.util.ArrayList;
import java.util.List;
@Configuration
@Slf4j
public class Jms {
    @Autowired
    RedisUtils redisUtils;
    private static final String HOTSPOTGOODS_ID = "hotspotGoodsId";
    /**
     * MQ监听消费
     * 封装了模糊删除的List方法
     * @param message 消息体
     * @throws JMSException
     * @throws JsonProcessingException
     */
    @JmsListener(destination = "GoodsDel",containerFactory = "containerFactory")
    public void GoodsDel(Message message) throws JMSException{
        //判断是否重复监听
        log.info("MQ监听器:监听到了GoodsDel");
        String jmsMessageID = message.getJMSMessageID();
        Object hget = redisUtils.hget(HOTSPOTGOODS_ID, jmsMessageID);
        if (!StringUtils.isEmpty(hget)){
            log.info("MQ监听:重复监听了=={}",jmsMessageID);
            message.acknowledge();
            return;
        }
        List<String> content = (List) ((ActiveMQObjectMessage) message).getObject();
        log.info("MQ监听器:接受到的消息是=={}",content);
        if (null==content||content.size()==0){
            log.info("MQ监听器:删除所有关于商品的消息");
            List<String> list = new ArrayList<>();
            list.add("allgoods");
            list.add("classification:");
            list.add("goodsName:");
            list.add("goodsOneKill");
            list.add("goodsListKill");
            list.add("gropuQueryList");
            list.add("queryTopList");
            list.add("querySelfList");
            list.add("class");
            redisUtils.delt(list);
        }else{
            redisUtils.delt(content);
        }
        //把完成完成的id添加到Redis--避免重复消费
        redisUtils.hset(HOTSPOTGOODS_ID,jmsMessageID,1);
        //提交
        message.acknowledge();
    }
}
